﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using System.Linq;
using DevExpress.XtraBars;
namespace QuanLyKhachSan
{
    public partial class frmSoDoPhong : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
        List<NavBarGroup> NavBarGroup = new List<NavBarGroup>();
        SimpleButton currbt;
        
        public frmSoDoPhong()
        {
            InitializeComponent();
        }
        
        public  void frmSoDoPhong_Load(object sender, EventArgs e)
        {
            this.FillData();
        }

        public  void FillData()
        {
            for (int i = 0; i < NavBarGroup.Count; i++)
            {
                NavBarGroup[i].Dispose();
            }
            DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
            //List<KHU_VUC> khuvuc = (from kv in db.KHU_VUCs select kv).ToList();
            var khuvuc = (from kv in db.KHU_VUCs select kv).ToList();
            for (int k = 0; k < khuvuc.Count; k++)
            {

                NavBarGroup group2 = new NavBarGroup();
                NavBarGroupControlContainer container2 = new NavBarGroupControlContainer();
                XtraScrollableControl panel2 = new XtraScrollableControl();

                navBarControl1.Controls.Add(container2);
                navBarControl1.Groups.Add(group2);

                group2.Caption = khuvuc[k].KV_TEN;
                
                group2.ControlContainer = container2;
                group2.AppearanceBackground.BackColor = System.Drawing.Color.WhiteSmoke;
                group2.AppearanceBackground.Options.UseBackColor = true;
                group2.Expanded = true;
                group2.GroupClientHeight = 200;
                group2.GroupStyle = DevExpress.XtraNavBar.NavBarGroupStyle.ControlContainer;
                group2.Name = "group" + k;
                
                NavBarGroup.Add(group2 );

                container2.Controls.Add(panel2);
                container2.Name = "container" + k;
                
                //container2.Size = new System.Drawing.Size(694, 78);
                container2.TabIndex = k;
                

                panel2.Dock = System.Windows.Forms.DockStyle.Fill;
                panel2.Location = new System.Drawing.Point(0, 0);
                panel2.Name = "panel" + k;
                //panel2.Dock = System.Drawing.Size(694, 78);
                panel2.TabIndex = 0;


                List<PHONG> phongs = (from p in db.PHONGs orderby p.P_ID ascending where p.KHU_VUC.KV_ID == khuvuc[k].KV_ID select p).ToList();
                List<GroupControl> Groups = new List<GroupControl>();
                List<PanelControl> Panels = new List<PanelControl>();
                List<SimpleButton> Buttons = new List<SimpleButton>();

                List<Label> Labels = new List<Label>();

               
                

                int x = 10;
                int y = 10;
                int xBt = 10;
                int yBt = 10;
                for (int i = 0; i < phongs.Count(); i++)
                {
                    GroupControl b = new GroupControl();
                   
                    b.Location = new Point(x, y);
                    x = x + 210;
                    if (x + 150 >= panel2.Width)
                    {
                        x = 10;
                        y = y + 170;
                    }
                    xBt = x;
                    yBt = y;
                 
                    b.Text = phongs[i].P_MA;

                    //b.Size = new System.Drawing.Size(85, 130);

                    b.Size = new System.Drawing.Size(200, 180);

                    b.AppearanceCaption.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
                    b.AppearanceCaption.Options.UseFont = true;
                    b.AppearanceCaption.Options.UseTextOptions = true;
                    b.AppearanceCaption.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;


                    SimpleButton button = new SimpleButton();

                    button.AllowFocus = false;
                    button.Appearance.BackColor = System.Drawing.Color.Transparent;
                    button.Appearance.Options.UseBackColor = true;
                    button.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
                    button.Dock = System.Windows.Forms.DockStyle.Fill;
                  
                    button.ImageList = this.imageCollectionPhong;
                    button.ImageLocation = DevExpress.XtraEditors.ImageLocation.MiddleCenter;
                    button.Location = new System.Drawing.Point(0, 0);
                    button.Size = new System.Drawing.Size(85,85);
                    button.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.UltraFlat;
                    button.Name = phongs [i].P_ID.ToString () ;


                    //load thông tin lên tooltip
                    string tooltip = "";

                    if (phongs[i].TT_ID == 5)
                    {
                        var chitiet = from c in db.CHI_TIET_THUE_PHONGs
                                      where c.P_ID == phongs[i].P_ID
                                      select c;
                        if (chitiet.Count() == 1)
                        {
                            var ngaythue = chitiet.Single();
                            tooltip = "Người Thuê : " + ngaythue.THUE_PHONG.KHACH_HANG.KH_HOTEN + "\n\n";
                            tooltip += "Ngày Thuê : " + ngaythue.CTTP_NGAYBD.ToString().Substring(0, 10) + "\n\n";
                            tooltip += "Giá : " + ngaythue.CTTP_DONGIA.ToString().TrimEnd('0').TrimEnd('.') + "\n\n";
                            tooltip += "Tên Khách : ";
                            //tim ten va so khac dang thue phong
                            var sokhach = (from t in db.TRU_TAIs
                                           where t.CTTP_ID == ngaythue.CTTP_ID
                                           select new { t.KHACH_HANG.KH_HOTEN, t.KH_ID }).ToList();
                            for (int kh = 0; kh < sokhach.Count; kh++)
                            {
                                tooltip += sokhach[kh].KH_HOTEN + ", ";

                            }
                            tooltip = tooltip.TrimEnd(' ').TrimEnd(',');
                            tooltip += "\n\n Số Khách : ";
                            if (sokhach.Count > 0)
                            {
                                tooltip += sokhach.Count;
                            }

                        } 

                    }
                        //neu trang thai la da dat
                    else if (phongs[i].TT_ID == 6)
                    {
                        var chitiet = from c in db.CHI_TIET_DAT_PHONGs
                                      join dg in db.DON_GIA_PHONGs 

                                      on new {c.PHONG.LP_ID , c.PHONG .CP_ID}
                                      equals new {dg.LP_ID , dg.CP_ID}

                                      where c.P_ID == phongs[i].P_ID
                                      select new { c.DAT_PHONG .KHACH_HANG .KH_HOTEN,
                                                   c.DAT_PHONG.DP_NGAYBD, dg.DG_GIA_NGAY,

                                      };
                        if (chitiet.Count() > 0)
                        {
                            var ngaydat = chitiet.Single();
                            tooltip = "Người Đặt : " + ngaydat.KH_HOTEN + "\n\n";
                            tooltip += "Ngày Đặt : " + ngaydat.DP_NGAYBD.ToString().Substring(0, 10) + "\n\n";
                            tooltip += "Giá : " + ngaydat.DG_GIA_NGAY.ToString().TrimEnd('0').TrimEnd('.') + "\n\n";
                            tooltip += "Tên Khách : ";
                            tooltip += "\n\n Giờ đến : " + ngaydat.DP_NGAYBD.Hour + ":" + ngaydat.DP_NGAYBD.Minute + "\n\n";
                            tooltip += "Số Khách : ";
                           

                        }

                    }



                    else
                    {
                        tooltip = "Nhấn để xem chi tiết \n" + phongs[i].P_MA;
                    }

                    //button.ToolTip = "Nhấn để xem chi tiết \n" + phongs[i].P_MA ;
                    button.ToolTip = tooltip;



                    //button.ImageIndex = 3;
                    //tam thoi khoa hien thi
                    //button.Text = phongs[i].TRANG_THAI_PHONG.TT_PHONG;

                    tooltip =  @"Người Đặt :     " + "NGUYEN VAN A" + "\n\n";
                    tooltip += @"Ngày Đặt :      " + "NGUYEN VAN A" + "\n\n";
                    tooltip += @"Giá :                  " + "NGUYEN VAN A" + "\n\n";
                    tooltip += @"Tên Khách :     " + "NGUYEN VAN A" + "\n\n";
                    tooltip += @"Giờ đến :          " + "NGUYEN VAN A" + "\n\n";
                    tooltip += @"Số Khách :       " + "NGUYEN VAN A";

                    button.Text = tooltip;

                    button.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Top;
                    button.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
                    button.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
                    button.ForeColor = System.Drawing.Color.FromArgb(int.Parse(phongs[i].TRANG_THAI_PHONG.TT_MAU));
                    barManager1.SetPopupContextMenu(button , popupMenu1);
                    
                    PanelControl panel = new PanelControl();
                   
                    //panel.Appearance.BackColor = System.Drawing.Color.FromArgb(int.Parse(phongs[i].TRANG_THAI_PHONG.TT_MAU));
                   
                    panel.Appearance.Options.UseBackColor = true;
                    panel.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                    //panel.Controls.Add(button);
                    panel.Dock = System.Windows.Forms.DockStyle.Fill;
                 
                    panel.Name = "panelControl" + i;

                    //hien thi du lieu len label
                    //Label labelText = new Label();
                    //Labels.Add(labelText);
                    //labelText.Text = phongs[i].TRANG_THAI_PHONG.TT_PHONG;
                    //panel.Controls.Add(labelText);

                    Buttons.Add(button);
                    Panels.Add(panel);
                    Groups.Add(b);

                    panel.Controls.Add(button);
                   

                    b.Controls.Add(panel);
                    panel2.Controls.Add(b);
                }





                

                // 
                // simpleButton1
                // 
                GroupControl groupThem = new GroupControl();
                PanelControl panelThem = new PanelControl();
                SimpleButton bt = new SimpleButton();



                groupThem.Location = new Point(xBt, yBt);
                groupThem.Text = "Thêm Phòng";
                groupThem.Size = new System.Drawing.Size(85, 130);



                panelThem.Appearance.BackColor = System.Drawing.Color.Transparent;
                panelThem.Appearance.Options.UseBackColor = true;
                panelThem.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                panelThem.Controls.Add(bt);
                panelThem.Dock = System.Windows.Forms.DockStyle.Fill;
                panelThem.Location = new System.Drawing.Point(2, 22);
                panelThem.Name = "panelControlThem" + k;

             

                bt.AllowFocus = false;
                bt.Appearance.BackColor = System.Drawing.Color.Transparent;
                bt.Appearance.Options.UseBackColor = true;
                bt.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
                bt.Dock = System.Windows.Forms.DockStyle.Fill;
                bt.Size = new System.Drawing.Size(85, 85);
                bt.ImageLocation = DevExpress.XtraEditors.ImageLocation.MiddleCenter;
                bt.Location = new System.Drawing.Point(0, 0);
                bt.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.UltraFlat;
                bt.Name =  khuvuc[k].KV_ID.ToString () ;
                bt.ToolTip = "Thêm phòng " + khuvuc[k].KV_TEN;
                bt.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Bottom;
                bt.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
                bt.ForeColor = System.Drawing.Color.Black;
                bt.Image = global::QuanLyKhachSan.Properties.Resources.add_room;
                
                
                panelThem.Controls.Add(bt);
                groupThem.Controls.Add(panelThem);
                panel2.Controls.Add(groupThem);

                bt.Click += new EventHandler(buttonThem_Click);

                for (int i = 0; i < Groups.Count; i++)
                {
                    SimpleButton button = Buttons[i];
                    //GroupControl g = Groups[i];
                    button.Click += new EventHandler(phong_Click);
                    button.MouseDown  += new MouseEventHandler(button_MouseDown);
                    button.MouseHover += new EventHandler(phong_MouseHover);
                }

            }
        }
        private void button_MouseDown(object sender, MouseEventArgs e)
        {
            currbt = (SimpleButton)sender;    
        }
        void phong_Click(object sender, EventArgs e)
        {
            //SimpleButton button = (SimpleButton)sender;
            ////GroupControl g = (GroupControl)sender;
            //frmChiTietPhong frmChiTiet = new frmChiTietPhong();
            //frmChiTiet.Text = "Chi Tiết Phòng " + button.Name;
            //frmChiTiet.ShowDialog();


            SimpleButton button = (SimpleButton)sender;
            frmPhong frmP = new frmPhong();

            // lấy thông tin phòng ban
            DataClassesKhachSanDataContext db1 = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
            var phong = (from p in db1.PHONGs where p.P_ID == int.Parse(button.Name) select p).Single();

            frmP.Text = "Chi Tiết Phòng " + phong.P_MA;
            frmP.txtMaPhong.Text = phong.P_MA;
            frmP.cbKhuVuc.EditValue = phong.KV_ID;
            frmP.cbKickCo.EditValue = phong.CP_ID;
            frmP.cbLoaiPhong.EditValue = phong.LP_ID;
            if (phong.TT_ID == 5)
            {
                frmP.cbTrangThai.EditValue = phong.TT_ID;
                frmP.cbTrangThai.Enabled = false;
            }
            else
            {
                frmP.cbTrangThai.EditValue = phong.TT_ID;
            }
            
            frmP.Add = false;
            frmP.pid = int.Parse(button.Name);
            frmP.btThem.Text = "Lưu";
            frmP.load_form += new EventHandler(load_form);
            frmP.ShowDialog();
        }

        //tao su kien mouse houve
        void phong_MouseHover(object sender, EventArgs e)
        {
            SimpleButton button = (SimpleButton)sender;

            //XtraForm1 frm = new XtraForm1();
            //frm.ShowDialog();
        }


        void buttonThem_Click(object sender, EventArgs e)
        {
            SimpleButton bt = (SimpleButton)sender;
            frmPhong frmP = new frmPhong();
           
            frmP.cbKhuVuc.EditValue = bt.Name ;

            //frmP .txtMaPhong.EditValue =  ;

            frmP.load_form += new EventHandler(load_form);
            frmP.ShowDialog();
        }

        


        void load_form(object sender, EventArgs e)
        {
            //for (int i = 0; i < NavBarGroup.Count; i++)
            //{
            //    NavBarGroup[i].Dispose();
            //}
            this.frmSoDoPhong_Load(this, EventArgs.Empty);
        }     

        private void frmSoDoPhong_ClientSizeChanged(object sender, EventArgs e)
        {
            //for (int i = 0; i < NavBarGroup.Count; i++)
            //{
            //    NavBarGroup[i].Dispose();
            //}

            frmSoDoPhong_Load(sender, e);
        }

        private void btDichVu_ItemClick(object sender, ItemClickEventArgs e)
        {
            //MessageBox.Show(currbt.Name);
            frmSuDungDichVu frmDichVu = new frmSuDungDichVu();
            //SimpleButton bt = (SimpleButton)sender;
           
            var kiemtra = db.Kiem_tra_Trang_thai_Thue_phong(int.Parse(currbt.Name)).ToList ();
            if (kiemtra.Count > 0 )
            { 
                var test = kiemtra.Single();
                if (test.TT_ID == 5)
                {
                   
                    frmDichVu .cttpid.Text = test.CTTP_ID.ToString ();
                    frmDichVu.frmsodo = this;
                    frmDichVu.ShowDialog();
                }
                else
                {
                    XtraMessageBox.Show("Phòng hiện tại không được thuê.");
                }
            }
           
            else
            {
                XtraMessageBox.Show("Phòng hiện tại không được thuê.");
            }

           
        }

        private void btXoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Tất cả những thông tin liên quan tới phòng sẽ bị xóa, bạn có muốn xóa ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (result == DialogResult.Yes)
            {
            try
            {
                var phong = from p in db.PHONGs
                            where p.P_ID == int.Parse(currbt.Name)
                            select p;
                db.PHONGs.DeleteAllOnSubmit(phong);
                db.SubmitChanges();
                this.load_form(this, EventArgs.Empty);
            }
            catch (Exception)
            {
                
                throw;
            }
            }
            
        }

        private void btThucAn_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmGoiThucAn frmthucan = new frmGoiThucAn();
            int ctid;
            var kiemtra = db.Kiem_tra_Trang_thai_Thue_phong(int.Parse(currbt.Name)).ToList();
            if (kiemtra.Count > 0)
            { 
                var test = kiemtra.Single();
                if (test.TT_ID == 5)
                {  
                   
                    frmthucan.cttpid.Text = test.CTTP_ID.ToString();
                    frmthucan.frmsodo = this;
                    frmthucan.ShowDialog();
                }
                else
                {
                    XtraMessageBox.Show("Phòng hiện tại không được thuê.");
                }
            }

            else
            {
                XtraMessageBox.Show("Phòng hiện tại không được thuê.");
            }
        }

        private void btCheckOut_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmCheckOut frmcheck = new frmCheckOut();
            var kiemtra = db.Kiem_tra_Trang_thai_Thue_phong( int.Parse(currbt.Name)).ToList();
            if (kiemtra.Count > 0)
            {
                var test = kiemtra.Single();
                if (test.TT_ID == 5)
                { 
                   
                    frmcheck.cttp_id = int.Parse ( test.CTTP_ID.ToString());
                    frmcheck.frmsodo = this;
                    frmcheck.ShowDialog();
                }
                else
                {
                    XtraMessageBox.Show("Phòng hiện tại không được thuê.");
                }
            }

            else
            {
                XtraMessageBox.Show("Phòng hiện tại không được thuê.");
            }         
        }

        private void btChuyenPhong_ItemClick(object sender, ItemClickEventArgs e)
        {


            frmChuyenPhong frmchuyen = new frmChuyenPhong();
            var kiemtra = db.Kiem_tra_Trang_thai_Thue_phong(int.Parse(currbt.Name)).ToList();
            if (kiemtra.Count > 0)
            {
                var test = kiemtra.Single();
                if (test.TT_ID == 5)
                {

                    frmchuyen.Text = "Chuyển từ " + test.P_MA;
                    frmchuyen.cttp_id = int.Parse(test.CTTP_ID.ToString());
                    frmchuyen.frmsodo = this;
                    frmchuyen.ShowDialog();
                }
                else
                {
                    XtraMessageBox.Show("Phòng hiện tại không được thuê.");
                }
            }

            else
            {
                XtraMessageBox.Show("Phòng hiện tại không được thuê.");
            }
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            this.FillData();
        }

        private void btThuePhong_ItemClick(object sender, ItemClickEventArgs e)
        {

            frmDangKyThuePhong frmthuephong = new frmDangKyThuePhong();
            frmthuephong.deNgayDen.EditValue = DateTime.Now;
            frmthuephong.deNgayDi.EditValue = DateTime.Now.AddDays(1);
            frmthuephong.PhongID = Convert .ToInt16 ( currbt.Name);
            //frmthuephong.lePhong.Enabled = false;
            frmthuephong.frmsodo = this;
            frmthuephong.testThuePhongtusodo = true;
            frmthuephong.ShowDialog();
        }

      
    }
}